package daemon;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

public class DeamonService {

	static {
//		String property = System.getenv("BASEDIR");
//		if(property==null) {
//			property=".";
//		}
//		System.out.println("using '"+property+"' as BASEDIR:");
		DOMConfigurator.configure("C:\\WorkSpace\\eclipseWorkSpace\\ec"+File.separator+"conf"
					+ File.separator + "log4j.xml");
		
	}
	public DeamonService(){
		logger.info("Adapter internal VersionNumber:2010.12.25.15.12");
		logger.info("Adapter internal Comment:xx");
		logger.debug("Adapter Initialized.");
	}
	private static Logger logger = Logger.getLogger(DeamonService.class);

	private void process() {
		ExecutorService es = Executors.newCachedThreadPool();

		logger.debug("Query thread number for Relationship is:");

		// es.submit(new DispatchTask());

		es.shutdown();

	}// end onInitialization..

	private static DeamonService adapter = new DeamonService();

	public static void main111(String[] args) throws FileNotFoundException,
			IOException, NumberFormatException, ClassNotFoundException,
			SQLException {
		logger.info("Main");
		isStart=true;
		new Thread(new Runnable() {
			public void run() {
				while (isStart) {
					logger.info("My Service Executing " + new java.util.Date());
						try {
							Thread.sleep(5000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
				}
			}
		}).start();
		
		
	}

	/**
	 * Static method called by prunsrv to start/stop the service. Pass the
	 * argument "start" to start the service, and pass "stop" to stop the
	 * service.
	 */
	private boolean stopped = false;

	private static Boolean isStart = false;

	public static void windowsService333(String args[]) {
		String cmd = "start";
		if (args.length > 0) {
			cmd = args[0];
		}
		logger.info("Adapter windows Service Cmd is " + cmd);
		synchronized(isStart) {
			if ("start".equals(cmd)) {
				if (isStart == false) {
					try {
						main111(new String[] {});
					} catch (NumberFormatException e) {
						e.printStackTrace();
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						e.printStackTrace();
					} catch (SQLException e) {
						e.printStackTrace();
					}
					isStart = true;
				} else {
					logger.info("Adapter already started");
				}
			} else if ("stop".equals(cmd)) {
				if (isStart = true) {
					System.exit(0);
				} else {
					logger.info("Adapter already stopped");
				}
			}}
	}
	   public static void windowsService(String args[]) {
		      String cmd = "start";
		      if(args.length > 0) {
		         cmd = args[0];
		      }
			
		      if("start".equals(cmd)) {
		         adapter.start();
		      }
		      else {
		    	  adapter.stop();
		      }
		   }

	public void start() {

		stopped = false;

		logger.info("My Service Started " + new java.util.Date());

		while (!stopped) {
			logger.info("My Service Executing " + new java.util.Date());
			synchronized (this) {
				try {
					this.wait(5000); // wait 1 minute
				} catch (InterruptedException ie) {
				}
			}
		}

		logger.info("My Service Finished " + new java.util.Date());
	}

	/**
	 * Stop this service instance
	 */
	public void stop() {
		stopped = true;
		synchronized (this) {
			this.notify();
		}
	}

}
